<?php

namespace app\services;

use plugin\admin\app\model\Text;

class TextService
{
    /**
     * 获取最热门的10条文章
     *
     * @param int $categoryId 分类ID
     * @return array
     */
    public static function getTop10ArticlesByCategoryId(int $categoryId): array
    {
        $articles = Text::query()
            ->where('category_id', $categoryId)
            ->select(['id', 'title', 'desc', 'img_url', 'en_title', 'en_desc', 'created_time', 'views'])
            ->orderBy('views', 'desc')
            ->where('is_hot', 1)
            ->limit(10)
            ->get()
            ->toArray();

        // 处理 img_url 和 created_time 字段
        foreach ($articles as &$article) {
            $article['img_url'] = full_url($article['img_url']);
            $article['created_time'] = date('Y-m-d', strtotime($article['created_time']));

            $article['title'] = get_current_language() == 'zh' ? $article['title'] : $article['en_title'];
            $article['desc'] = get_current_language() == 'zh' ? $article['desc'] : $article['en_desc'];
        }

        return $articles;
    }

    /**
     * 获取文章列表根据分类ID
     *
     * @param int $categoryId 分类ID
     * @param int $page 页码
     * @param int $pageSize 页长
     * @param string|null $keyword 关键词
     * @return array
     */
    public static function getArticlesByCategoryId(int $categoryId, int $page, int $pageSize, ?string $keyword = null): array
    {
        $query = Text::query()
            ->where('category_id', $categoryId)
            ->where('is_hot', 0)
            ->select(['id', 'title', 'desc', 'img_url', 'en_title', 'en_desc',  'created_time', 'views']);

        if (!empty($keyword)) {
            $query->where('title', 'like', '%' . $keyword . '%');
        }

        $offset = ($page - 1) * $pageSize;
        $total = $query->count(); // 获取总记录数
        $articles = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('sort', 'asc')
            ->get()
            ->toArray();

        // 处理 img_url 和 created_time 字段
        foreach ($articles as &$article) {
            $article['img_url'] = full_url($article['img_url']);
            $article['created_time'] = date('Y-m-d', strtotime($article['created_time']));

//            $article['title'] = get_current_language() == 'zh' ? $article['title'] : $article['en_title'];
//            $article['desc'] = get_current_language() == 'zh' ? $article['desc'] : $article['en_desc'];
        }

        return [
            'articles' => $articles,
            'pagination' => [
                'current_page' => $page,
                'page_size' => $pageSize,
                'total' => $total,
                'total_pages' => ceil($total / $pageSize),
            ],
        ];
    }

    public static function getArticleDetail(int $id): ?array
    {
        $article = Text::query()
            ->where('id', $id)
            ->select(['id', 'title', 'desc', 'en_desc', 'views', 'en_title', 'en_content', 'sort', 'img_url', 'content', 'category_id', 'created_time'])
            ->first();

        if ($article) {
            $article->increment('views');

            $article = $article->toArray();
            $article['img_url'] = full_url($article['img_url']);
            $article['created_time'] = date('Y-m-d', strtotime($article['created_time']));

            $article['content'] = replace_content($article['content']);


            // 获取上一篇文章
            $prevArticle = Text::query()
                ->where('category_id', $article['category_id'])
                ->where('sort', '<', $article['sort'])
                ->orderBy('sort', 'desc')
                ->select(['id', 'title'])
                ->first();

            // 获取下一篇文章
            $nextArticle = Text::query()
                ->where('category_id', $article['category_id'])
                ->where('sort', '>', $article['sort'])
                ->orderBy('sort', 'asc')
                ->select(['id', 'title'])
                ->first();

            $article['prev_article'] = $prevArticle ? $prevArticle->toArray() : null;
            $article['next_article'] = $nextArticle ? $nextArticle->toArray() : null;

            return $article;
        }

        return null;
    }

}